package view;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

import server.Log;

/**
 * Class used as pool: contains all system definitions.
 */
public class Settings implements Serializable
{
	/**
	 * 
	 */
	private static final long serialVersionUID = -400244250039297898L;

	/* User mode */ 
	static protected String userMode = UserMode.standartUser;
	
	/* Settings file */
	static private String fileName = "settings";
	
	/********************************* Logger ************************************/
	/* Default depth of the logger in lines */
	protected static final int DEFAULT_LOGGER_WINDOW_DEPTH = 20;
	protected static int windowDepth = DEFAULT_LOGGER_WINDOW_DEPTH;
	
	/* Default depth of the logger in days */
	protected static final int DEFAULT_LOGGER_HISTORY_DEPTH = 1;
	
	/* Actual depth of the logger */
	protected static int historyDepth = DEFAULT_LOGGER_HISTORY_DEPTH;

	/* Option for the last filtering */
	
	/* Parameter for the last filtering */
	protected static FilterType currentFilterType = FilterType.ALL;
	
	protected static Integer filterPort = null ;
	protected static String filterIP    = "" ;
	protected static Log.directOpt filterDir ;
	protected static String filterProtocol = "" ;
	
	protected static boolean loggerOn = true;
	
	/********************************* Rules ************************************/
	/* Default depth of the rules tables in lines */
	protected static final int DEFAULT_RULES_WINDOW_DEPTH = 15;
	
	/* Current ruleset id.
	 * -1 - current FW ruleset
	 */
	protected static String ruleset = "default";
	
	/* Actual ruleset rules number */
	protected static int inRulesNumber = 0;
	protected static int outRulesNumber = 0;
	
	/********************************* FW ************************************/
	/* Parameter for FW status */
	protected static boolean fwTurnedOn = true;

	public static Integer statsDenied   = 0;
	public static Integer statsAccepted = 0;
	
	protected static void save() throws IOException
	{
		/* Open output stream */
		FileOutputStream fos = new FileOutputStream(fileName);
		ObjectOutputStream oos = new ObjectOutputStream(fos);

		/* Save the data */

	    oos.writeObject(new String(Settings.filterIP));
	    oos.writeObject(new String(Settings.filterProtocol));
	     oos.writeObject(new Integer(Settings.historyDepth));
	    oos.writeObject(new Boolean(Settings.fwTurnedOn));
	    
		/* Close output stream */	    
	    oos.flush();
	    oos.close();
	    fos.close();
	}
	
	protected static void load() throws IOException, ClassNotFoundException
	{
		/* Open input stream */
		FileInputStream fis = new FileInputStream(fileName);
		ObjectInputStream ois = new ObjectInputStream(fis);

		/* Save the data */
		Settings.filterIP = (String) ois.readObject();
		Settings.filterProtocol = (String)ois.readObject();
		Settings.historyDepth = ((Integer)ois.readObject()).intValue();
		Settings.fwTurnedOn = ((Boolean)ois.readObject()).booleanValue();
		
		/* Close output stream */
		ois.close();
		fis.close();	
	}
}
